#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/1/11 16:50
# @Author  : Beam
# @Site    : 
# @File    : monitors.py
# @Software: PyCharm


from django.shortcuts import render,HttpResponse
from django.views import View
import json
import datetime,time
from config import setting
from api import models
from autocmdb.models import Hardware
from django.utils.dateparse import parse_datetime

class Monitor(View):
	def get(self,request,*args,**kwargs):
		username = request.session.get('username')
		minion_id = setting.SALTMINIONSID
		minions_id = Hardware.objects.all().values('minion_id')
		begintime = datetime.datetime.now() + datetime.timedelta(hours=-2)
		# cputimes = models.CpuTimes.objects.filter(create_time__gte = begintime).filter(minions_id = setting.SALTMINIONSID).order_by('create_time')
		cpuload  = models.CpuLoad.objects.filter(create_time__gte = begintime).filter(minions_id = setting.SALTMINIONSID).order_by('create_time')
		sysparameters = models.SysParameters.objects.filter(create_time__gte = begintime).filter(minions_id = setting.SALTMINIONSID).order_by('create_time')
		memeryminions = models.MemeryMinions.objects.filter(create_time__gte = begintime).filter(minions_id = setting.SALTMINIONSID).order_by('create_time')
		diskio = models.DiskIO.objects.filter(create_time__gte = begintime).filter(minions_id = setting.SALTMINIONSID).order_by('create_time')
		networkio = models.NetworkIO.objects.filter(create_time__gte = begintime).filter(minions_id = setting.SALTMINIONSID).order_by('create_time')
		netsstat = models.NetStat.objects.filter(create_time__gte = begintime).filter(minions_id = setting.SALTMINIONSID).order_by('create_time')
		# print('---------------',cputimes,type(cputimes))
		# print(test)
		# for i in test:
		# 	print(i.create_time,i.nowpercent,i.loadfiften)
		# return render(request, 'monitor.html', {'username': username, 'cpuload': cpuload})
		return render(request, 'monitor.html',{'username':username,'minion_id':minion_id,'minions_id':minions_id,'cpuload':cpuload,
		                                       'sysparameters':sysparameters,
		                                       'memeryminions':memeryminions,'diskio':diskio,'networkio':networkio,
		                                       'netsstat':netsstat})

	def post(self,request,*args,**kwargs):
		username = request.session.get('username')
		minions_id = Hardware.objects.all().values('minion_id')
		minion_id = request.POST.get('minion_db_id',None)
		# begintime = parse_datetime(request.POST.get('begintime',None))
		# endtime = parse_datetime(request.POST.get('endtime',None))
		begintime = request.POST.get('begintime',None)
		endtime = request.POST.get('endtime',None)
		if not begintime:
			begintime = datetime.datetime.now() + datetime.timedelta(hours=-2)
		if not endtime:
			endtime = datetime.datetime.now() + datetime.timedelta()
			print(begintime,endtime)
		cpuload = models.CpuLoad.objects.filter(create_time__range=(begintime, endtime)).filter(minions_id=minion_id).order_by('create_time')
		sysparameters = models.SysParameters.objects.filter(create_time__range=(begintime, endtime)).filter(minions_id=minion_id).order_by('create_time')
		memeryminions = models.MemeryMinions.objects.filter(create_time__range=(begintime, endtime)).filter(minions_id=minion_id).order_by('create_time')
		diskio = models.DiskIO.objects.filter(create_time__range=(begintime, endtime)).filter(minions_id=minion_id).order_by('create_time')
		networkio = models.NetworkIO.objects.filter(create_time__range=(begintime, endtime)).filter(minions_id=minion_id).order_by('create_time')
		netsstat = models.NetStat.objects.filter(create_time__range=(begintime, endtime)).filter(minions_id=minion_id).order_by('create_time')
		return render(request, 'monitor.html',{'username':username,'minion_id':minion_id,'minions_id':minions_id,'cpuload':cpuload,
		                                       'sysparameters':sysparameters,
		                                       'memeryminions':memeryminions,'diskio':diskio,'networkio':networkio,
		                                       'netsstat':netsstat})
